
package com.rx.mgt.dao;  

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.rx.mgt.dao.base.ManagementHibernateDaoBase;
import com.rx.mgt.domain.AdminUser;
import com.rx.mgt.po.UsersBo;

/** 
 * ClassName:AdminUserDao 
 * Function :
 * Reason   : 
 * Date     :Jan 31, 2017 1:42:43 PM 
 * @author   luolei 
 * @since    JDK 1.6
 */
@Repository
public class AdminUserDao  extends ManagementHibernateDaoBase<AdminUser, Long>{
    
    @Transactional(readOnly=true)
    public Long getAdminUserCount(UsersBo usersBo){
        String hql= " select count(*) from AdminUser au where 1=1";
        if(StringUtils.isNotBlank(usersBo.getStartDate())){
            hql=hql +" and  unix_timestamp(au.create_time)>unix_timestamp(:startTime)";
        }
        Query query = template.getSessionFactory().getCurrentSession().createQuery(hql);
        if(StringUtils.isNotBlank(usersBo.getStartDate())){
            query.setParameter("startTime", usersBo.getStartDate());
        }
        return (Long) query.uniqueResult();
    }
    
    @Transactional(readOnly=true)
    public AdminUser getAdminUserByPsw(AdminUser adminUser){
        String hql= " from AdminUser au where au.user_name=:user_name and au.user_password=:user_password ";
        Query query = template.getSessionFactory().getCurrentSession().createQuery(hql);
        query.setParameter("user_name", adminUser.getUser_name());
        query.setParameter("user_password", adminUser.getUser_password());
        return (AdminUser) query.uniqueResult();
    }

    @Transactional(readOnly=true)
    public List<AdminUser> getAllAdminUser(String type){
        String hql= " from AdminUser au where au.type=:type";
        Query query = template.getSessionFactory().getCurrentSession().createQuery(hql);
        query.setParameter("type", type);
        return query.list();
    }

    @Transactional(readOnly=true)
    public List getAdminUserNameByName(String name) {
        String hql = "from AdminUser au where au.user_name=:name";
        Query query = template.getSessionFactory().getCurrentSession().createQuery(hql);
        query.setParameter("name", name);
        return query.list();
    }
}
  